<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Your Settings</h1>

          <form @submit.prevent="updateUser">
            <fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control"
                  type="text"
                  placeholder="URL of profile picture"
                  v-model="userInfo.image"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Your Name"
                  v-model="userInfo.username"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  class="form-control form-control-lg"
                  rows="8"
                  placeholder="Short bio about you"
                  v-model="userInfo.bio"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Email"
                  v-model="userInfo.email"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control form-control-lg"
                  type="password"
                  placeholder="Password"
                  v-model="userInfo.password"
                />
              </fieldset>
              <button class="btn btn-lg btn-primary pull-xs-right">Update Settings</button>
            </fieldset>
          </form>
          <hr />
          <button class="btn btn-outline-danger" @click="logout">Or click here to logout.</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { updateUser } from "@/api/user";
const Cookie = process.client ? require("js-cookie") : undefined;

export default {
  middleware: ["authenticated"],
  name: "setting",
  computed: {
    ...mapState(["user"]),
    userInfo() {
      const user = this.user || {};
      return {
        username: user.username,
        bio: user.bio,
        email: user.email,
        image: user.image,
        password: ""
      };
    }
  },
  methods: {
    ...mapMutations(["setUser"]),
    async updateUser() {
      const { data } = await updateUser(this.userInfo);
      this.setUser(data.user);
      this.$router.push(`/profile/${this.user.username}`);
    },
    logout() {
      this.setUser(null);
      Cookie.remove("user");
      this.$router.push(`/`);
    }
  }
};
</script>